package com.bw.z.mvp.view.people;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.z.R;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.view.BaseFragment;
import com.bw.z.mvp.contract.NewsContract;
import com.bw.z.mvp.di.DaggerNewsComponent;
import com.bw.z.mvp.di.NewsModules;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.RecommendEntity;
import com.bw.z.mvp.presenter.NewsPresenter;
import com.bw.z.mvp.view.news.NewsValuesActivity;
import com.bw.z.mvp.view.people.adapter.RecommendAdapter;
import com.bw.z.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendFragment extends BaseFragment<NewsPresenter>
        implements NewsContract.NewView, OnItemChildClickListener, OnRefreshLoadMoreListener {

    private SmartRefreshLayout recommendSmart;
    private RecyclerView recommendRv;
    private RecommendAdapter adapter;
    private LinearLayoutManager manager;
    private List<RecommendEntity.Values> list = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView() {
        recommendSmart = (SmartRefreshLayout) f(R.id.recommend_smart);
        recommendSmart.setEnableLoadMore(true);
        recommendSmart.setEnableRefresh(true);
        recommendSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        recommendSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        recommendSmart.setOnRefreshLoadMoreListener(this);
        recommendRv = (RecyclerView) f(R.id.recommend_rv);
        manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recommendRv.setLayoutManager(manager);
        adapter = new RecommendAdapter();
        adapter.setOnItemChildClickListener(this);
        recommendRv.setAdapter(adapter);
    }

    @Override
    public void inject() {
        DaggerNewsComponent.builder().newsModules(new NewsModules(this)).build().injectRecommendNews(this);
    }

    @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        if (baseEntity instanceof RecommendEntity) {
            RecommendEntity recommendEntity = (RecommendEntity) baseEntity;
            adapter.setNewInstance(recommendEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        RecommendEntity recommendEntity = (RecommendEntity) baseEntity;
        if (recommendEntity.getValues() == null) {
            ToastUtils.showMsg("刷新失败");
            return;
        }
        adapter.setNewInstance(recommendEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        RecommendEntity recommendEntity = (RecommendEntity) baseEntity;
        if (recommendEntity.getValues() == null) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        if (recommendEntity.getValues().size() == 0) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        list = adapter.getData();
        list.addAll(recommendEntity.getValues());
        adapter.setNewInstance(list);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<RecommendEntity.Values> list = (List<RecommendEntity.Values>) adapter.getData();
        String news_url = list.get(position).news_url;
        long news_id = list.get(position).news_id;
        Bundle bundle = new Bundle();
        bundle.putString("news_url", news_url);
        bundle.putLong("news_id", news_id);
        startPage(bundle, NewsValuesActivity.class);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestLoad(newsMap);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        if (list != null)
            list.clear();
        Map<String, Object> newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestRefresh(newsMap);
    }
}